﻿using Nest;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;

namespace EsDemo.Es
{
    public class VisitLogRepository : ElasticsearchRepositoryBase, IVisitLogRepository
    {
        public VisitLogRepository(IElasticsearchProvider elasticsearchProvider) : base(elasticsearchProvider)
        {
        }

        protected override string IndexName => "visitlogs";

        public async Task InsertAsync(VisitLog visitLog)
        {
            await Client.IndexAsync(visitLog, x => x.Index(IndexName));
        }
        public async Task InsertOtherAsync(VisitLog visitLog)
        {
            await Client.IndexAsync(visitLog, x => x.Index("Other"));
        }

        public async Task DeleteAsync(string id)
        {
            await Client.DeleteAsync<VisitLog>(id, x => x.Index(IndexName));
        }

        public async Task UpdateAsync(VisitLog visitLog)
        {
            await Client.UpdateAsync<VisitLog>(visitLog.Id, x => x.Index(IndexName).Doc(visitLog));
        }

        public async Task<Tuple<int, IList<VisitLog>>> QueryAsync(int page, int limit)
        {
            var query = await Client.SearchAsync<VisitLog>(x => x.Index(IndexName)
                                    .From((page - 1) * limit)
                                    .Size(limit)
                                    .Sort(x => x.Descending(v => v.CreatedAt)));
            return new Tuple<int, IList<VisitLog>>(Convert.ToInt32(query.Total), query.Documents.ToList());
        }
        public async Task<Tuple<int, IList<VisitLog>>> QueryListAsync(int page, int limit, string fildStr)
        {

            //var starTime = DateTime.Now.AddDays(-30);
            //var endTime = DateTime.Now;
            //fildStr = $"*{fildStr}*";
            //QueryContainer query1 = new TermQuery { Field ="Id" , Value = fildStr };
            //QueryContainer query2 = new TermQuery { Field = "UserAgent", Value = fildStr };
            //query1 = query1 || query2;
            //var query = await Client.SearchAsync<VisitLog>(x => x.Index(IndexName)
            //                       .Query(y=>
            //                            (y.Match(z=>z.Field(o=>o.UserAgent).Query(fildStr))
            //                            ||
            //                            y.Match(z=>z.Field(o=>o.Id).Query(fildStr)))
            //                            &&
            //                            y.DateRange(z=>
            //                                z.Field(o=>o.CreatedAt).GreaterThanOrEquals(starTime).LessThan(endTime)))
            //                       .From((page - 1) * limit)
            //                       .Size(limit)
            //                       .Sort(x => x.Descending(v => v.CreatedAt)));
            //return new Tuple<int, IList<VisitLog>>(Convert.ToInt32(query.Total), query.Documents.ToList());

            var starTime = DateTime.Now.AddDays(-30);
            var endTime = DateTime.Now;
            fildStr = $"*{fildStr}*";
            QueryContainer query1 = new TermQuery { Field = "Id", Value = fildStr };
            QueryContainer query2 = new TermQuery { Field = "UserAgent", Value = fildStr };
            query1 = query1 || query2;


            var query = await Client.SearchAsync<VisitLog>(x => x.Index(IndexName)
                                   .Query(y =>
                                        (y.Match(z => z.Field(o => o.UserAgent).Query(fildStr))
                                        ||
                                        y.Match(z => z.Field(o => o.Id).Query(fildStr)))
                                        &&
                                        y.DateRange(z =>
                                            z.Field(o => o.CreatedAt).GreaterThanOrEquals(starTime).LessThan(endTime)))
                                   .From((page - 1) * limit)
                                   .Size(limit)
                                   .Sort(x => x.Descending(v => v.CreatedAt)));
            return new Tuple<int, IList<VisitLog>>(Convert.ToInt32(query.Total), query.Documents.ToList());
        }
    }
}
